<template>
  <div class="login_content">
    <div class="login_atator">
      <div class="login_logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="0px"
        class="login_form"
        :rules="loginFormRules"
        size="medium"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="loginResetFields">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loginResetFields() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("/login", this.loginForm);
        //console.log(res);
        if (res.meta.status != 200) return this.$message.error("登录失败");

        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>
<style lang="less">
.login_content {
  width: 100%;
  height: 100%;
  background: #2f4f6f;

  .login_atator {
    width: 450px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    background-color: #fff;
    transform: translate(-50%, -50%);
    border-radius: 4px;

    .login_logo {
      width: 110px;
      height: 110px;
      border-radius: 50%;
      border: 1px #eee solid;
      padding: 10px;
      position: absolute;
      left: 50%;
      top: 0%;
      background-color: #fff;
      transform: translate(-50%, -50%);
      box-shadow: 0px 0px 10px #898b8d;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .login_form {
      position: absolute;
      top: 90px;
      padding: 20px;
      width: 100%;
      box-sizing: border-box;
    }
  }
}
</style>
